<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .el-table .warning-row {
            background: oldlace;
        }

        .el-table .success-row {
            background: #f0f9eb;
        }
    </style>
    <script src="js/vue.js"></script>
    <script src="js/axios-0.18.0.js"></script>
    <script src="element-ui/lib/index.js"></script>
    <link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">
    <script>
        window.onload = function () {
            new Vue({
                el: "#app",
                mounted() {
                    /*var _this = this
                    axios.get("http://localhost:8080/brand-case/brand/selectAll").then(function (resp) {
                        _this.tableData = resp.data;
                    })*/
                    this.selectAll();
                },
                data() {
                    return {
                        pageSize:5,
                        totalData:100,
                        currentPage: 1,
                        brand: {
                            status: '',
                            brandName: '',
                            companyName: '',
                            id: '',
                            statu: '',
                            description: ''
                        },
                        multipleSelection: [],
                        tableData: [
                            {
                                id: '1',
                                brandName: '小米',
                                companyName: '小米科技有限公司',
                                ordered: '2',
                                status: '1'
                            }, {
                                id: '2',
                                brandName: '小米',
                                companyName: '小米科技有限公司',
                                ordered: '2',
                                status: '0'
                            }, {
                                id: '3',
                                brandName: '小米',
                                companyName: '小米科技有限公司',
                                ordered: '2',
                                status: '0'
                            }, {
                                id: '4',
                                brandName: '小米',
                                companyName: '小米科技有限公司',
                                ordered: '2',
                                status: '1'
                            }],

                        dialogFormVisible: false,
                        dialogFormVisible1: false,
                        formLabelWidth: '120px'
                    }
                },
                methods: {
                    selectAll() {
                        var _this = this;
                        axios.post("http://localhost:8080/brand-case/brand/selectByPageAndCondition?currentPage="+this.currentPage+"&pageSize="+this.pageSize,this.brand).then(function (resp) {
                            _this.tableData = resp.data.rows;
                            _this.totalData = resp.data.totalCount;
                        })
                    },
                    deleteById(id) {
                        var _this = this;
                        console.log(id);
                        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning'
                        }).then(() => {
                            axios.get("http://localhost:8080/brand-case/brand/deleteById?id=" + id).then(function (resp) {
                                _this.selectAll();
                            })
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                        }).catch(() => {
                            this.$message({
                                type: 'info',
                                message: '已取消删除'
                            });
                        });
                    },
                    updateBrandById() {
                        var _this = this;
                        console.log(_this.brand.id)
                        axios.post("http://localhost:8080/brand-case/brand/updateBrandById", _this.brand).then(function (resp) {
                            _this.dialogFormVisible = false;
                            _this.selectAll();
                            _this.$message({
                                message: '恭喜你，修改成功！',
                                type: 'success'
                            })
                        })

                    },
                    tableRowClassName({row, rowIndex}) {
                        if (rowIndex === 1) {
                            return 'warning-row';
                        } else if (rowIndex === 3) {
                            return 'success-row';
                        }
                        return '';
                    },
                    handleSelectionChange(val) {
                        this.multipleSelection = val;
                        // console.log(val)
                    },
                    onSubmit() {
                        // console.log(this.brand);
                        this.selectAll();
                    },
                    addBrand() {
                        // this.dialogFormVisible=false;
                        // console.log(this.brand)
                        var _this = this;
                        axios.post("http://localhost:8080/brand-case/brand/add", _this.brand).then(function (resp) {
                            console.log(resp.data)
                            if (resp.data == "success") {
                                _this.dialogFormVisible1 = false;
                                _this.selectAll();
                                _this.$message({
                                    message: '恭喜你，添加成功！',
                                    type: 'success'
                                })
                            }
                        })
                    },
                    handleSizeChange(val) {
                        console.log(`每页 ${val} 条`);
                        this.pageSize = val;
                        this.selectAll();
                    },
                    handleCurrentChange(val) {
                        console.log(`当前页: ${val}`);
                        this.currentPage = val;
                        this.selectAll();
                    },
                    selectOne(id) {
                        this.brand.id = id;
                        this.dialogFormVisible = true;
                        var _this = this;
                        axios.get("http://localhost:8080/brand-case/brand/selectOne?id=" + this.brand.id).then(function (resp) {
                            console.log(resp.data)
                            _this.brand = resp.data;
                        })
                    },
                    //批量删除
                    deleteByIds() {
                        var _this = this;
                        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning'
                        }).then(() => {
                            var ids_length = _this.multipleSelection.length
                            var ids = []
                            for (let i = 0; i < _this.multipleSelection.length; i++) {
                                ids[i] = _this.multipleSelection[i].id
                            }
                            axios.post("http://localhost:8080/brand-case/brand/deleteByIds", ids).then(function (resp) {
                                if (resp.data == "success") {
                                    _this.selectAll();
                                }
                            })
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                        }).catch(() => {
                            this.$message({
                                type: 'info',
                                message: '已取消删除'
                            });
                        });
                    },
                    // selectByPage(){
                    //     param = {
                    //         currentPage: this.currentPage,
                    //         size:this.size
                    //     }
                    //     axios.post("http://localhost:8080/brand-case/brand/deleteByIds", ids).then(function (resp) {
                    //         if (resp.data == "success") {
                    //             _this.selectAll();
                    //         }
                    //     })
                    // }
                }
            })
        }
    </script>
</head>
<body>
<div id="app">
    <el-form :inline="true" :model="brand" class="demo-form-inline">
        <el-form-item label="当前状态">
            <el-select v-model="brand.status" placeholder="当前状态">
                <el-option label="启动" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="企业名称">
            <el-input v-model="brand.brandName" placeholder="企业名称"></el-input>
        </el-form-item>
        <el-form-item label="品牌名称">
            <el-input v-model="brand.companyName" placeholder="品牌名称"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
    </el-form>
    <el-row>
        <el-button type="primary" plain @click="dialogFormVisible1 = true">新增</el-button>
        <el-button type="warning" plain @click="deleteByIds">批量删除</el-button>
    </el-row>

    <el-dialog title="编辑品牌" :visible.sync="dialogFormVisible1">
        <el-form ref="form" :model="brand" label-width="80px">
            <el-form-item label="品牌名称">
                <el-input v-model="brand.brandName"></el-input>
            </el-form-item>
            <el-form-item label="企业名称">
                <el-input v-model="brand.companyName"></el-input>
            </el-form-item>
            <el-form-item label="排序">
                <el-input v-model="brand.ordered"></el-input>
            </el-form-item>
            <el-form-item label="备注">
                <el-input type="textarea" v-model="brand.description"></el-input>
            </el-form-item>
            <el-form-item label="状态">
                <el-switch v-model="brand.status" active-value="1"
                           inactive-value="0"></el-switch>
            </el-form-item>


            <el-form-item>
                <el-button type="primary" @click="addBrand">提交</el-button>
                <el-button @click="dialogFormVisible1 = false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>

    <template>
        <el-table
                :data="tableData"
                style="width: 100%"
                :row-class-name="tableRowClassName"
                @selection-change="handleSelectionChange">
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column
                    type="index"
                    prop="id"
                    width="50">
            </el-table-column>
            <el-table-column
                    prop="brandName"
                    label="品牌名称"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="companyName"
                    label="企业名称"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="description"
                    label="描述"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="ordered"
                    align="center"
                    label="排序">
            </el-table-column>
            <el-table-column
                    prop="statusStr"
                    align="center"
                    label="当前状态">
            </el-table-column>
            <el-table-column
                    align="center"
                    label="操作">
                <template slot-scope="scope">
                    <el-row>

                        <el-button type="primary" @click="selectOne(scope.row.id)">修改</el-button>
                        <el-button type="warning" @click="deleteById(scope.row.id)">删除</el-button>
                    </el-row>
                </template>
            </el-table-column>
        </el-table>
    </template>
    <el-dialog title="修改" :visible.sync="dialogFormVisible">
        <el-form ref="form" :model="brand" label-width="80px">
            <el-form-item label="品牌名称">
                <el-input v-model="brand.brandName"></el-input>
            </el-form-item>
            <el-form-item label="企业名称">
                <el-input v-model="brand.companyName"></el-input>
            </el-form-item>
            <el-form-item label="排序">
                <el-input v-model="brand.ordered"></el-input>
            </el-form-item>
            <el-form-item label="备注">
                <el-input type="textarea" v-model="brand.description"></el-input>
            </el-form-item>
            <el-form-item label="状态">
                <el-switch v-model="brand.status" active-value="1"
                           inactive-value="0" active-color="green" inactive-color="orange">brand.status
                </el-switch>
            </el-form-item>


            <el-form-item>
                <el-button type="primary" @click="updateBrandById()">提交</el-button>
                <el-button @click="dialogFormVisible = false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalData">
    </el-pagination>
</div>
</body>
</html>